package playsManager.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

import playsManager.*;
import playsManager.domainLogic.DomainLogic;
import playsManager.utilities.Utilities;

class TheaterMenu extends UserMenu implements ActionListener, TableModelListener{

	protected TheaterLogon theater;

	protected JDialog dialog;
	protected JButton viewPlays;
	protected JButton commit;
	protected JButton cancel;
	protected JButton addPlay;
	@SuppressWarnings("rawtypes")
	protected JComboBox genre;
	protected JTextField numDates;
	protected JTextField dates;
	protected JTextField title;
	protected JTextField description;
	protected JPanel p;

	public TheaterMenu(TheaterLogon theater, DomainLogic theDl)
	{
		super(theDl);

		this.theater = theater;
	}

	@Override
	protected void InitMenu() {

		//add controls to viewer menu

		p = new JPanel();
		viewPlays = new JButton("View Plays");
		viewPlays.addActionListener(this);
		commit = new JButton("Add new play");
		commit.addActionListener(this);
		p.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		menuFrame.add(p);
		p.add(viewPlays);
		p.add(commit);
	}

	ArrayList<PlayGuiAdapter> thePlays;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(viewPlays)){
			menuFrame.removeAll();
			p.removeAll();
			String[] columnNames = {"Title", "Theater", "Start Date", "End Date", "Genre", "Description", "Delete"};
			
			ArrayList<Play> dbPlays;
			try {
				dbPlays = dl.GetTheaterPlays(theater.id);
			
				for(Play p : dbPlays)
					thePlays.add(new PlayGuiAdapter(p));
				
				Object[][] data = new String[thePlays.size()][7];
				for(int i = 0; i < thePlays.size(); i++)
				{
					PlayGuiAdapter thePlay = thePlays.get(i);
					data[i][0] = thePlay.GetName();
					data[i][1] = thePlay.GetTheaterLocation();
					ArrayList<Date> dates = thePlay.GetDates();
					data[i][2] = dates.get(0);
					data[i][3] = dates.get(dates.size()-1);
					data[i][4] = thePlay.GetGenre();
					data[i][5] = thePlay.GetDescription();
					data[i][6] = new Boolean(false);
				}
				JTable t = new JTable(data, columnNames);
				t.getModel().addTableModelListener(this);
				dialog = new JDialog(menuFrame, "Plays", true);
				dialog.add(t);
				dialog.setVisible(true);
			//menuFrame.add(p);
			} catch (Exception e1) {
				Utilities.ShowErrorMessage("error loading plays.");
				e1.printStackTrace();
			}
		}
		else if(e.getSource().equals(commit)){
			//menuFrame.removeAll();
			p.removeAll();
			String genres[] = {"Genre", "Comedy", "Drama", "History", "Romance"};
			addPlay = new JButton("OK");
			cancel = new JButton("Cancel");
			addPlay.addActionListener(this);
			cancel.addActionListener(this);
			genre = new JComboBox(genres);
			numDates = new JTextField("Number of show dates");
			dates = new JTextField("Show dates: MM/DD/YYYY, MM/DD/YYYY");
			title = new JTextField("Play Title");
			description = new JTextField("Play Description");
			p.add(title);
			p.add(description);
			p.add(numDates);
			p.add(dates);
			p.add(genre);
			p.add(addPlay);
			p.add(cancel);
			dialog = new JDialog(menuFrame, "Add New Play", true);
			dialog.setSize(400, 200);
			dialog.add(p);
			dialog.setVisible(true);
			//menuFrame.add(p);

		}
		else if(e.getSource().equals(cancel)){
			dialog.setVisible(false);
		}
		else if(e.getSource().equals(addPlay)){
			String titleString, genreType, descriptionStyle;
			ArrayList<Date> Dates = new ArrayList<Date>();
			if(title.getText().compareTo("Play Title") == 0 || title.getText().compareTo("Must have a title!") == 0 || title.getText().compareTo("") == 0){
				title.setText("Must have a title!");
			}
			else{
				titleString = title.getText();
				if(genre.getSelectedItem().toString().compareTo("Genre") != 0){
					genreType = genre.getSelectedItem().toString();
					if(numDates.getText().charAt(0) > 57 || (numDates.getText().charAt(0) < 48 && numDates.getText().charAt(0) != '-')){
						numDates.setText("Must have at least 1 Date!");
					}
					else{
						if(Integer.parseInt(numDates.getText()) < 1){
							numDates.setText("Must have at least 1 Date!");
						}
						else{
							for(int i = 0; i < Integer.getInteger(numDates.getText()); i++)
							{
								try {
									Dates.add(DateFormat.getInstance().parse(dates.getText().substring(0, dates.getText().indexOf(','))));
									dates.setText(dates.getText().substring(dates.getText().indexOf(',')));
								} catch (ParseException e1) {
									Utilities.ShowErrorMessage("error adding date.");
									e1.printStackTrace();
								}
							}
							if(description.getText().compareTo("Play Description") == 0){
								descriptionStyle = "A play";
							}
							else{
								descriptionStyle = description.getText();
							}
						
							Play dbPlay = new Play();
							PlayGuiAdapter thePlay = new PlayGuiAdapter(dbPlay);
							
							thePlay.SetName(titleString);
							thePlay.SetDates(Dates);
							thePlay.SetDescription(descriptionStyle);
							thePlay.SetGenre(genreType);
							
							try {
								dl.AddPlay(dbPlay);
							} catch (Exception e1) {
								Utilities.ShowErrorMessage("error adding play.");
								e1.printStackTrace();
							}
							
							dialog.setVisible(false);
						}
					}
				}
			}
		}
	}

	@Override
	public void tableChanged(TableModelEvent arg0) {
		if(arg0.getColumn() == 6){
			try {
				dl.DeletePlay(thePlays.get(arg0.getFirstRow()).GetId());
			} catch (Exception e) {
				Utilities.ShowErrorMessage("error deleting play.");
				e.printStackTrace();
			}
		}
	}

}
